#include "string.h"
#include "bitmap.h"
#include "containers.h"
#include "algorithm.h"

void test_queue()
{
    struct queue_t *queue = init_queue();
    enqueue(queue, 1);
    enqueue(queue, 2);
    enqueue(queue, 3);
    enqueue(queue, 4);

    for (int i = dequeue(queue); i != CONTAINER_EMPTY; i = dequeue(queue))
        printf("%d\n", i);
}

void test_stack_ops()
{
    struct stack_t *stack;
    stack = init_stack();

    push_stack(stack, 1);
    push_stack(stack, 2);
    push_stack(stack, 3);
    push_stack(stack, 4);

    for (int i = pop_stack(stack); i != CONTAINER_EMPTY; i = pop_stack(stack))
        printf("%d\n", i);
}

void test_bitmap_init()
{
    struct bitmap_t *map = create_bitmap();
    set_bitmap(map, 2);
    if (is_bitmap_set(map, 2))
        printf("Set\n");
    else
        printf("Zero\n");
}

void test_bitmap_find()
{
    // create bitmap
    struct bitmap_t *map = create_bitmap();
    // target to find in bitmap

    uint32_t key = 3;
    // source array
    const uint32_t array[] = {1, 2, 18, 15, 19, 4, 8, 9, 11, 5, 4, 3, 16};
    int array_len = sizeof(array) / 4;

    // initialize bit map
    for (int i = 0; i < array_len; i++)
        set_bitmap(map, array[i]);

    // search in bitmap
    if (is_bitmap_set(map, key))
        printf("Yes\n");
    else
        printf("No\n");
}

void test_string_init()
{
    struct string_t *string = init_string((const unsigned char *)"123456789", 9);
    print_string(string);
    struct string_t *sub = sub_string(string, 5, 2);
    print_string(sub);
    if (compare_string(string, sub))
        printf("equel\n");
    else
        printf("not equel\n");

    int index = find_string(string, sub);
    if (index)
        printf("founded at: %d\n", index);
    else
        printf("Not matched\n");
}

void test_array_move()
{
    int array[10] = {1, 2, 3, 4, 5, 6};
    int array_len = 10;
    array_pos_insert(array, array_len, 2, 20);

    for (int i = 0; i < array_len; i++)
        printf("--%d--", array[i]);

    printf("\n");
}

void test_find_repeated_number()
{
    int array[] = {1, 2, 2, 3, 4, 5, 5, 5, 6, 7, 7, 8};
    int len = 12;
    int counts = find_repeated_number(array, len);
    printf("counts: %d\n", counts);

    for (int i = 0; i < len; i++)
        printf("-[%d]-", array[i]);

    printf("\nComplete.\n");
}

void test_brute_force_match_string()
{
    struct string_t *source = init_string((const unsigned char *)"brute force algorithm", 21);
    struct string_t *pattern = init_string((const unsigned char *)"force", 5);
    int pos = brute_force_match_algo(source, pattern);
    printf("POS: %d\n", pos);
}

void test_insert_sort()
{
    int array[] = {2, 8, 4, 1, 9, 3, 7, 6, 5, 0};
    int len = ARRAY_LEN(array);
    insert_sort(array, len);

    for (int index = 0; index < len; index++)
        printf(" [%d] ", array[index]);

    printf("\nComplete.\n");
}

void test_sequential_search()
{
    int array[] = {2, 3, 4, 5, 6, 7, 8, 8};
    int target = 20;
    int pos = sequential_search(array, target, ARRAY_LEN(array));

    if (pos >= 0)
        printf("target %d found in position %d;\n", target, pos);
    else
        printf("target is not included in array.\n");
}

void test_binary_search()
{
    int array[] = {2, 3, 4, 6, 9, 12, 14, 15, 20, 24, 39, 40, 50};
    int target = 2;
    int pos = binary_search(array, target, ARRAY_LEN(array));

    if (pos >= 0)
        printf("target %d found in position %d;\n", target, pos);
    else
        printf("target is not included in array.\n");
}

void test_quick_sort()
{
    int array[] = {2, 3, 9, 1, 12, 23, 90, 65, 32, 51, 87, 64, 48, 100, 200, 94, 0, 81};
    quick_sort(array, ARRAY_LEN(array));

    for (int index = 0; index < ARRAY_LEN(array); index++)
        printf(" [%d] ", array[index]);
    printf("\n");
}

void test_shell_sort()
{
    int array[] = {2, 3, 9, 1, 12, 23, 90, 65, 32, 51, 87, 64, 48, 100, 200, 94, 0, 81};
    shell_sort(array, ARRAY_LEN(array));

    for (int index = 0; index < ARRAY_LEN(array); index++)
        printf(" [%d] ", array[index]);
    printf("\n");
}

void test_bubble_sort(){
      int array[] = {2, 3, 9, 1, 12, 23, 90, 65, 32, 51, 87, 64, 48, 100, 200, 94, 0, 81};
    bubble_sort(array, ARRAY_LEN(array));

    for (int index = 0; index < ARRAY_LEN(array); index++)
        printf(" [%d] ", array[index]);
    printf("\n");
}